#pragma once

#include <iostream>

#include "../comm/Log.hpp"
#include "../comm/Util.hpp"

#include <assert.h>
#include <vector>
#include <unordered_map>
#include <fstream>

namespace ns_model
{
    using namespace ns_util;
    using namespace ns_log;

    struct Question
    {

        std::string number;     //  唯一的题目编号
        std::string title;      //  题目标题
        std::string difficulty; //  题目难度: 简单 中等 困难
        int cpulimit;           //  时间限制(单位:s)
        int memlimit;           //  内存限制(单位:kb)
        std::string desc;       //  题目描述
        std::string header;     //  预设代码
        std::string tail;       //  测试用例
    };

    const std::string questions_list = "./questions/questions.list";
    const std::string question_path = "./questions/";

    class Model
    {
    public:
        Model()
        {
            assert(LocalQuestionList(questions_list));
        }

        bool LocalQuestionList(const std::string &question_list)
        {
            std::ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL, "配置文件加载失败,其检查题库是否存在\n");
                return false;
            }

            std::string line;
            //  1 判断回文数 简单 1 30000
            while (std::getline(in, line))
            {
                std::vector<std::string> tokens;
                StringUtil::SplitString(line, &tokens, " ");
                if (tokens.size() != 5)
                {
                    LOG(WARNING, "加载部分题库失败,请检查题库格式\n");
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.difficulty = tokens[2];
                q.cpulimit = stoi(tokens[3]);
                q.memlimit = stoi(tokens[4]);

                std::string path = question_path + q.number + "/";
                q.desc = FileUtil::ReadFile(path + "desc.txt", true);
                q.header = FileUtil::ReadFile(path + "header.cpp", true);
                q.tail = FileUtil::ReadFile(path + "tail.cpp", true);

                questions.insert({q.number, q});
            }

            LOG(INFO, "题库加载成功！\n");
            in.close();
            return true;
        }

        bool GetAllQuestions(std::vector<Question> *out)
        {
            if (questions.size() == 0)
            {
                LOG(ERROR, "用户获取题库失败\n");
                return false;
            }
            for (const auto &q : questions)
            {
                out->push_back(q.second);
            }
            return true;
        }

        bool GetOneQuestions(const std::string &number, Question *question)
        {
            const auto &iter = questions.find(number);
            if (iter == questions.end())
            {
                LOG(ERROR, "用户获取[%s]号题目失败\n", number.c_str());
                return false;
            }

            *question = iter->second;
            return true;
        }

        ~Model()
        {
        }

    private:
        std::unordered_map<std::string, Question> questions;
    };
}